package com.hao.dispatcher;

import com.hao.model.QueueManager;
import com.hao.model.PCBProcess;

import java.util.Comparator;
import java.util.LinkedList;

/**
 * 优先级调度
 */
public class PriorityFirstDispatcher implements Dispatcher {

    @Override
    public PCBProcess doDispatcher(QueueManager queueManager) {
        if (queueManager.getReadyQueue().size() <= 0) {
            return null;
        }

        LinkedList<PCBProcess> readyQueue = queueManager.getReadyQueue();
        readyQueue.sort(new Comparator<PCBProcess>() {
            @Override
            public int compare(PCBProcess pcbProcess1, PCBProcess pcbProcess2) {
                return pcbProcess2.getPri() - pcbProcess1.getPri();//优先级从大到小
            }
        });

        PCBProcess pcbProcess =  readyQueue.poll();

        return pcbProcess;
    }
}
